package com.ecarx.camera.worker.car.aosp;

import android.annotation.SuppressLint;
import android.car.VehiclePropertyIds;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.ecarx.camera.utils.LogUtils;
import com.ecarx.camera.utils.Utils;
import com.ecarx.camera.worker.car.IDeviceInfoProxy;

/**
 * 设备信息接口实现
 */
public class DeviceInfoProxyAospImpl implements IDeviceInfoProxy {

    private static final String TAG = "DeviceInfoProxyAospImpl";

    @Override
    public String getVehicleType() {
        return Build.MODEL;
    }

    @Override
    public String getProjectCode() {
        return Build.PRODUCT;
    }

    @Override
    public String getSupplierCode() {
        return Build.MANUFACTURER;
    }

    @Override
    public String getOperatorName() {
        return Build.BRAND;
    }

    @Override
    public String getVin() {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "getVin but carPropertyManager is null");
            return null;
        }
        CarPropertyValue<String> carPropertyValue = carPropertyManager.getProperty(VehiclePropertyIds.INFO_VIN, AospPropertyMapping.DEFAULT_ZONE);;
        return carPropertyValue == null ? null : carPropertyValue.getValue();
    }

    @SuppressLint("MissingPermission")
    @Override
    public String getICCID() {
        Context context = Utils.getApp();
        if (context == null) {
            LogUtils.e(TAG, "getICCID null, cause: Application not init");
            return null;
        }
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager == null) {
            LogUtils.e(TAG, "getICCID null, cause: TelephonyManager is null");
            return null;
        }
        return telephonyManager.getSimSerialNumber();
    }
}
